/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.spider.comic.service.impl;

import me.zhengjie.spider.comic.domain.SpiderComicContent;
import me.zhengjie.spider.comic.service.dto.SpiderComicContentDto;
import me.zhengjie.spider.comic.service.mapstruct.SpiderComicContentMapper;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.spider.comic.repository.SpiderComicContentRepository;
import me.zhengjie.spider.comic.service.SpiderComicContentService;
import me.zhengjie.spider.comic.service.dto.SpiderComicContentQueryCriteria;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author 雨来
* @date 2023-07-22
**/
@Service
@RequiredArgsConstructor
public class SpiderComicContentServiceImpl implements SpiderComicContentService {

    private final SpiderComicContentRepository spiderComicContentRepository;
    private final SpiderComicContentMapper spiderComicContentMapper;

    @Override
    public PageResult<SpiderComicContentDto> queryAll(SpiderComicContentQueryCriteria criteria, Pageable pageable){
        Page<SpiderComicContent> page = spiderComicContentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(spiderComicContentMapper::toDto));
    }

    @Override
    public List<SpiderComicContentDto> queryAll(SpiderComicContentQueryCriteria criteria){
        return spiderComicContentMapper.toDto(spiderComicContentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SpiderComicContentDto findById(Long comicContentId) {
        SpiderComicContent spiderComicContent = spiderComicContentRepository.findById(comicContentId).orElseGet(SpiderComicContent::new);
        ValidationUtil.isNull(spiderComicContent.getComicContentId(),"SpiderComicContent","comicContentId",comicContentId);
        return spiderComicContentMapper.toDto(spiderComicContent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SpiderComicContent resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setComicContentId(snowflake.nextId()); 
        spiderComicContentRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SpiderComicContent resources) {
        SpiderComicContent spiderComicContent = spiderComicContentRepository.findById(resources.getComicContentId()).orElseGet(SpiderComicContent::new);
        ValidationUtil.isNull( spiderComicContent.getComicContentId(),"SpiderComicContent","id",resources.getComicContentId());
        spiderComicContent.copy(resources);
        spiderComicContentRepository.save(spiderComicContent);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long comicContentId : ids) {
            spiderComicContentRepository.deleteById(comicContentId);
        }
    }

    @Override
    public void download(List<SpiderComicContentDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SpiderComicContentDto spiderComicContent : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("父id", spiderComicContent.getComicDetailId());
            map.put("图片地址", spiderComicContent.getImg());
            map.put("本地路径", spiderComicContent.getLocalPath());
            map.put("排序", spiderComicContent.getSortNum());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void createBatch(List<SpiderComicContent> spiderComicContentList) {
        spiderComicContentRepository.saveAll(spiderComicContentList);
    }

    @Override
    public int countByComicDetailId(Long comicDetailId) {
        return spiderComicContentRepository.countByComicDetailId(comicDetailId);
    }
}